﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PerformanceTest
{
    internal sealed class OperationTimer : IDisposable
    {
        private Int64 m_startTime;
        private string m_text;
        private Int32 m_collectionount;
        public OperationTimer(string text)
        {
            PrepareForOperation();
            m_text = text;
            m_collectionount = GC.CollectionCount(0);
            m_startTime = Stopwatch.GetTimestamp();
        }
        public void Dispose()
        {
            Console.WriteLine("{0,6:###.00} seconds (GCs={1,3})  {2}",
                (Stopwatch.GetTimestamp() - m_startTime) / (Double)Stopwatch.Frequency,
            GC.CollectionCount(0) - m_collectionount, m_text);

        }
        private static void PrepareForOperation()
        {
            GC.Collect();
            GC.WaitForPendingFinalizers();
            GC.Collect();
        }
    }
}
